<?php
namespace app\index\channel;


use app\api\job\PlayerReport;
use app\common\model\Gameulog;
use app\common\model\Player;
use app\index\controller\Exception;
use think\cache\driver\Redis;
use think\Db;
use think\facade\Log;
use think\facade\Session;
use app\index\controller\Login;
use think\Queue;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Gaode
{

    protected $key = "ddceb237dd37a9e5";
    protected $gamekey = "add4cbdcdebd2f55d33c5ad6f3360236";
    /**
     * 注册
     */
    public function register($params,$gid,$cid){
        //签名
        if(!$this->check_sign($params,$this->key)){
            return false;
        }
        $player = Player::getOne(['account'=>"g_".$params['uid']]);
        //注册
        $data['account'] = "g_".$params['uid'];
        $data['password'] = get_encrypt('123456');
        $data['type'] = 0;
        $data['cid'] = $cid;
        $data['gid'] = $gid;
        if(!$player){
            try {
                Db::startTrans();
                $player = Player::register($data);
                Db::commit();
            }catch (Exception $e) {
                Db::rollback();
                Log::info("用户注册失败:".$e->getMessage());
            }
        }else{
            if($player['gid'] == "0"){
                $player['gid'] = $gid;
                $player->save();
            }
        }
        $data['uid'] = $player['id'];
        $uid = $player['id'];

        //记录登录信息
        Gameulog::addLog($data);
        $token = get_encrypt($data['uid']);
        $player['token'] = $token;
        $time = time();
        $player['login_time'] = $time;
        $player['update_time'] = $time;
        unset($player['password']);
        $player['rp_token'] = $params['rp_token'];
        //redis 保存用户信息
        $redis = new Redis();
        $redis->set($token, $player);

//        $grr = [30];
//        if(in_array($data['gid'], $grr)){
//            $player_old = DB::connect('db_config_aiqi')->name('player_old')->where(['banma_id'=>$data['uid']])->find();
//            if($player_old){
//                $uid = $player_old['banma_id'];
//            }
//        }
        $redis->set('channel_data_'.$uid, $params);
        return $player;
    }

    /**
     * 定时任务上报数据
     * @param $params
     */
    public function push_report($rp_token){
        $url = "https://restapi.amap.com/ws/activity/basic/task_center/task_report";
        $url = $url."?".http_build_query(['rp_token'=>$rp_token,'key'=>$this->gamekey]);
        return file_get_contents($url);
    }

    /**
     * 支付成功订单上报
     * @param $order
     * @return null
     */
    public function order_report($order){
        $token = get_encrypt($order['uid']);
        $redis = new Redis();
        $player = $redis->get($token);
        Log::info("高德上报订单用户信息：".json_encode($player));
        if(!empty($player['rp_token'])){
            $url = "https://restapi.amap.com/ws/activity/points_mall/game_charge_report";
            $data = [
                'rp_token'=>$player['rp_token'],
                'key'=>$this->gamekey,
                'order_id'=>$order['order_no'],
                'price'=>$order['amount'] * 100,
                ];
            Log::info("高德上报订单：".json_encode($data));
            $url = $url."?".http_build_query($data);
            $res = file_get_contents($url);
            Log::info("高德上报订单返回数据：".json_encode($res));
        }
    }

    private function check_sign($params,$key)
    {
        unset($params[1]);
        $sign = $params['sign'];
        unset($params['sign']);
        unset($params['rp_token']);
        if(!empty($params['token'])){
            unset($params['token']);
        }
        $check_sign = get_sign($params,$key);
        if($check_sign != $sign){
            return false;
        }
        return true;
    }

}